package cn.edu.scau.cmi.zhangjunming.client;

import java.util.List;
import java.util.Scanner;

import cn.edu.scau.cmi.zhangjunming.hibernate.DAO.StudentDAO;
import cn.edu.scau.cmi.zhangjunming.hibernate.DAO.TeacherDAO;
import cn.edu.scau.cmi.zhangjunming.hibernate.domain.Student;
import cn.edu.scau.cmi.zhangjunming.hibernate.domain.Teacher;

public class hibernateClient {

	public static void main(String args[]) {
		Student student = new Student();
		Teacher teacher = new Teacher();
		List<Student> stuList = null;
		List<Teacher> teaList = null;
		StudentDAO studentDAO = new StudentDAO();
		TeacherDAO teacherDAO = new TeacherDAO();
		Scanner scanner = new Scanner(System.in);
		int order =1;
		int num;
		int num1;
		String name;
		while(order>0) {
			System.out.println("请选择指令：\n1.添加学生\n2.修改学生\n3.删除学生");
			System.out.println("4.添加教师\n5.修改教师\n6.删除教师");
			System.out.println("7.学生添加导师\n8.学生修改导师\n9.教师选择学生\n0.退出");
			order = scanner.nextInt();
			switch(order) {
			case 1:
				//System.out.println("请输入学生号码：");
				//num  = scanner.nextInt();
				System.out.println("请输入学生姓名：");
				name = scanner.next();
				//student.setId(num);
				student.setName(name);
				studentDAO.save(student);
				break;
			case 2:
				stuList = studentDAO.listAll();
				if(stuList.size()==0) {
					System.out.println("学生表为空");
				    break;
				}
				System.out.println("列表序号     id      姓名");
				for(int i = 0;i<stuList.size();i++) {
					System.out.println(i+"     | "+stuList.get(i).toString());
				}
				System.out.println("请输入要修改的学生序号：");
				num = scanner.nextInt();
				if(num<0||num>=stuList.size()) {
					System.out.println("输入学生序号非法!");
				    break;
				}
				student = stuList.get(num);
				//System.out.println("请输入要修改的学生编号");
				//num1 = scanner.nextInt();
				//student.setId(num1);
				System.out.println("请输入要修改的学生名字：");
				name = scanner.next();
				student.setName(name);
				studentDAO.update(student);
				break;
			case 3:
				stuList = studentDAO.listAll();
				if(stuList.size()==0) {
					System.out.println("学生表为空");
				    break;
				}
				System.out.println("列表序号     id      姓名");
				for(int i = 0;i<stuList.size();i++) {
					System.out.println(i+"     | "+stuList.get(i).toString());
				}
				System.out.println("请输入要删除的学生序号：");
				num = scanner.nextInt();
				if(num<0||num>=stuList.size()) {
					System.out.println("输入学生序号非法!");
				    break;
				}
				student = stuList.get(num);
				studentDAO.delete(student);
				break;
			case 4:
				System.out.println("请输入教师号码：");
				num  = scanner.nextInt();
				System.out.println("请输入教师姓名：");
				name = scanner.next();
				teacher.setId(num);
				teacher.setName(name);
				teacherDAO.save(teacher);
				break;
			case 5:
				teaList = teacherDAO.listAll();
				if(teaList.size()==0) {
					System.out.println("教师表为空");
					break;
				}
				
				System.out.println("列表序号     id      姓名");
				for(int i = 0;i<teaList.size();i++) {
					System.out.printf("%d   |", i);
					teaList.get(i).show();
				}
				System.out.println("请输入要修改的教师序号：");
				num = scanner.nextInt();
				teacher = teaList.get(num);
				
				if(num<0||num>=teaList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				
				System.out.println("请输入要修改的教师编号：");
				num1 = scanner.nextInt();
				teacher.setId(num1);
				System.out.println("请输入要修改的教师名字：");
				name = scanner.next();
				teacher.setName(name);
				teacherDAO.update(teacher);
				break;
			case 6:
				teaList = teacherDAO.listAll();
				if(teaList.size()==0) {
					System.out.println("教师表为空");
					break;
				}
				
				System.out.println("列表序号      id     姓名");
				for(int i=0; i<teaList.size();i++) {
					System.out.printf("%d   |", i);
					teaList.get(i).show();
				}
				
				System.out.println("请输入要修改的教师序号：");
				num = scanner.nextInt();
				
				
				if(num<0||num>=teaList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				
				teacher = teaList.get(num);
				teacherDAO.delete(teacher);
				break;
			case 7:
				stuList = studentDAO.listAll();
				if(stuList.size()==0) {
					System.out.println("学生表为空");
					break;
				}
				for(int i=0;i<stuList.size();i++) {
				    student = stuList.get(i);
				    System.out.println(i+student.toString());
				    if(student.getTutor()!=null) {
				    	System.out.println(student.getTutor().getId()+" "+student.getTutor().getName());
				    }
				}
				System.out.println("请输入要修改的学生序号：");
				num = scanner.nextInt();
				if(num<0||num>stuList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				student = stuList.get(num);
				
				teaList = teacherDAO.listAll();
				
				if(teaList.size()==0) {
					System.out.println("教师表为空！");
					break;
				}
				for(int i=0;i<teaList.size();i++) {
					System.out.printf("  %d  |",i);
					teaList.get(i).show();
				}
				System.out.println("请输入要的教师序号：");
				num = scanner.nextInt();
				if(num<0||num>=teaList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				teacher = teaList.get(num);
				System.out.println(teacher.getName());
				student.setTutor(teacher);
				studentDAO.update(student);
				break;
			case 8:
				stuList = studentDAO.listAll();
				if(stuList.size()==0) {
					System.out.println("学生表为空");
					break;
				}
				for(int i=0;i<stuList.size();i++) {
				    student = stuList.get(i);
				    System.out.println(i+student.toString());
				    if(student.getTutor()!=null) {
				    	System.out.println(student.getTutor().getId()+" "+student.getTutor().getName());
				    }
				}
				System.out.println("请输入要修改的学生序号：");
				num = scanner.nextInt();
				if(num<0||num>stuList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				student = stuList.get(num);
				
				teaList = teacherDAO.listAll();
				
				if(teaList.size()==0) {
					System.out.println("教师表为空！");
					break;
				}
				for(int i=0;i<teaList.size();i++) {
					System.out.printf("  %d  |",i);
					teaList.get(i).show();
				}
				System.out.println("请输入要的教师序号：");
				num = scanner.nextInt();
				if(num<0||num>=teaList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				teacher = teaList.get(num);
				student.setTutor(teacher);
				studentDAO.update(student);
				break;
			case 9:
                teaList = teacherDAO.listAll();
				
				if(teaList.size()==0) {
					System.out.println("教师表为空！");
					break;
				}
				for(int i=0;i<teaList.size();i++) {
					System.out.printf("  %d  |",i);
					teaList.get(i).show();
				}
				System.out.println("请输入要的教师序号：");
				num = scanner.nextInt();
				if(num<0||num>=teaList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				teacher = teaList.get(num);
				
				stuList = studentDAO.listAll();
				if(stuList.size()==0) {
					System.out.println("学生表为空");
					break;
				}
				for(int i=0;i<stuList.size();i++) {
				    student = stuList.get(i);
				    System.out.println(i+student.toString());
				    if(student.getTutor()!=null) {
				    	System.out.println(student.getTutor().getId()+" "+student.getTutor().getName());
				    }
				}
				System.out.println("请输入要修改的学生序号：");
				num = scanner.nextInt();
				if(num<0||num>stuList.size()) {
					System.out.println("输入数据非法！");
					break;
				}
				student = stuList.get(num);
				student.setTutor(teacher);
				studentDAO.update(student);
				break;
			case 0:
				break;
			default:
					System.out.println("No match order!");
			}
		}
	}
	

}
